<?php
/**
 * 布局插件
 * 用来控制器渲染视图的时候使用布局
 *
 * @author zhoushen
 * @since 2015/10/14
 */
class LayoutPlugin extends Yaf\Plugin_Abstract {

	public function routerShutdown(\Yaf\Request_Abstract $request, \Yaf\Response_Abstract $response) {
        $dispatcher_config = \Yaf\Application::app()->getConfig()->application;
        $modules = explode(",",$dispatcher_config['modules']);
        $current_module = $request->getModuleName();
        if(!in_array($current_module,$modules) || $current_module == "Index"){
		    $request->setModuleName($dispatcher_config['dispatcher']['defaultModules']);
        }
	}
	public function postDispatch(Yaf\Request_Abstract $request, Yaf\Response_Abstract $response) {
		if(isset($response->layout) && !empty($response->getBody()) ){
			$response->setBody( call_user_func( $response->layout, $response->getBody() ) );
		}
	}
}
